package com.wx.mapper;

import com.github.pagehelper.Page;
import com.wx.dto.BookPageQueryDTO;
import com.wx.dto.BookTypeDTO;
import com.wx.entity.Book;
import com.wx.entity.BookType;
import com.wx.vo.BookQueryVO;
import com.wx.vo.BookTypeVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface BookMapper {
    //查询图书编号是否存在
    @Select("select * from book where book_no = #{bookNo}")
    Book queryByBookNo(String bookNo);

    //添加图书
    @Insert("insert into " +
            "book(book_name, book_type, book_no, author, description, image, amount, publisher, publish_date, add_time, add_user, update_time, update_user) " +
            "values(#{bookName},#{bookType},#{bookNo},#{author},#{description},#{image},#{amount},#{publisher},#{publishDate},#{addTime},#{addUser},#{updateTime},#{updateUser})")
    Integer addbook(Book book);

    //根据分类名查询分类
    @Select("select * from booktype where name = #{name}")
    BookType getTypeByName(String name);

    //根据id查询分类
    @Select("select * from booktype where id = #{id}")
    BookType getTypeById(Integer id);

    //插入分类
    @Insert("insert into " +
            "booktype(name, create_time, create_user, sort, update_time, update_user) " +
            "values(#{name},#{createTime},#{createUser},#{sort},#{updateTime},#{updateUser})")
    Integer addtype(BookType bookType);

    //查询分类信息
    @Select("select * from booktype")
    List<BookType> getType();

    //根据图书分类查询图书
    @Select("select count(*) from book where book_type = #{id}")
    Integer queryBookByBookType(Integer id);

    //条件修改分类信息
    Integer updatetype(BookType bookType);

    //根据id删除分类
    @Delete("delete from booktype where id = #{id}")
    Integer deletetype(Integer id);

    //图书分页查询
    //Page<BookQueryVO> pageQuery(BookPageQueryDTO bookPageQueryDTO);
    List<BookQueryVO> pageQuery(BookPageQueryDTO bookPageQueryDTO);

    //查询总图书记录
    Integer queryBookCount(BookPageQueryDTO bookPageQueryDTO);

    //查询图书数量
    @Select("select amount from book where book_no = #{bookId}")
    Integer getCountById(Integer bookId);

    // 图书数量减一
    @Update("update book set amount = amount - 1 where book_no = #{bookId}")
    Integer deleteOneBook(Integer bookId);

    // 图书数量加一
    @Update("update book set amount = amount + 1 where book_no = #{bookId}")
    Integer increaseOneBook(Integer bookId);


    //查询图书Id是否存在
    @Select("select book_name, book_type, book_no, author, description, image, amount, publisher, publish_date from book where id = #{id}")
    Book queryBookById(Integer id);


    /*管理员端图书分类查询*/
    List<BookTypeVO> getTypeVO(BookTypeDTO bookTypeDTO);

    /*查询图书是否有人借阅*/
    @Select("select count(*) from borrow where book_id=#{bookNo} and is_finish != 1")
    Integer examinebook(Book book);

    /*删除图书*/
    @Delete("delete from book where book_no=#{bookNo}")
    void deleBook(Book book);

    /*更新图书信息，by图书编号*/
    @Update("update book set amount=#{amount},description=#{description},update_time=#{updateTime},update_user=#{updateUser} where book_no = #{bookNo}")
    void updateBookByBookNo(Book book);
}
